<template>
	<view class="u-wrap">
		<swiper class="swip-container" :indicator-dots="showIndicator" :circular="isCircular" indicator-color="#fff"
			indicator-active-color="#000000">
			<swiper-item v-for="item in list">
				<image class="imgs" :src="item.image"></image>
			</swiper-item>
		</swiper>

		<view class="search-container">
			<u-search class="search" placeholder="请输入关键字" :show-action="false"
				bg-color="#fff" :animation="true" @change="searchList" v-model="keywords"></u-search>
		</view>

		<view class="waterfall">
			<u-button @click="clear">清空列表</u-button>
			<u-waterfall v-if="flowList.length > 0" v-model="flowList" ref="uWaterfall1">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index">
						<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
						<u-lazy-load threshold="-450" border-radius="10" :image="item.image"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.goodsName}}
						</view>
						<view class="demo-price">
							{{item.goodsPrice}}元
						</view>
						<view class="demo-tag">
							<view v-if="item.type=='0'" class="demo-tag-owner">
								闲置
							</view>
							<view style="margin-left: 0;" v-else class="demo-tag-text">
								求购
							</view>
						</view>
						<!-- <u-icon name="close-circle-fill" color="#fa3534" size="34" class="u-close"
							@click="remove(item.id)"></u-icon> -->
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index">
						<u-lazy-load threshold="-450" border-radius="10" :image="item.image"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.goodsName}}
						</view>
						<view class="demo-price">
							{{item.goodsPrice}}元
						</view>
						<view class="demo-tag">
							<view v-if="item.type=='0'" class="demo-tag-owner">
								闲置
							</view>
							<view style="margin-left: 0;" v-else class="demo-tag-text">
								求购
							</view>
						</view>
						<!-- <u-icon name="close-circle-fill" color="#fa3534" size="34" class="u-close"
							@click="remove(item.id)"></u-icon> -->
					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus"></u-loadmore>
		</view>
	</view>
</template>

<script setup>
	// 选项式
	/* export default {
		data() {
			return {
				showIndicator: true,
				isCircular:true,
				list: [{
						image: '/static/33.jpeg',
					},
					{
						image: '/static/44.jpg',
					},
					{
						image: '/static/66.jpeg',
					}
				],
			}
		},
		onLoad() {},
		methods: {},
	} */
	// 组合式
	import {
		ref,
		onMounted
	} from 'vue';
	// 引入api
	import {
		getIndexListApi
	} from '../../api/index.js'
	
	import {
		onReady,
		onReachBottom
	} from '@dcloudio/uni-app';
	
	const showIndicator = ref(true)
	const isCircular = ref(true)

	const list = ref([{
			image: '/static/33.jpeg',
		},
		{
			image: '/static/44.jpg',
		},
		{
			image: '/static/66.jpeg',
		}
	])

	//瀑布流
	const flowList = ref([])

//加载更多
	const loadStatus = ref('loadmore')
	// 点击查询参数
	const currentPage = ref(1) //当前页数
	const pageSize = ref(5) //每页查询条数
	const pages = ref(0) //总页数
	const keywords = ref('') //查询关键字

	// 读取推荐到首页的商品数据
	const getIndexList = async () => {
		let res = await getIndexListApi({
			currentPage: currentPage.value,
			pageSize: pageSize.value,
			keywords: keywords.value
		})
		console.log(res);
		if (res && res.code == 200) {
			pages.value = res.data.pages //设置总页数
			// 设置瀑布流数据
			flowList.value = flowList.value.concat(res.data.records);
			loadStatus.value = 'loadmore';
		}
	}
	
	// 瀑布流表单对象
	const uWaterfall1 = ref()
	
	// 搜索功能
	const searchList = () => {
		uWaterfall1.value.clear() //清空瀑布流
		currentPage.value = 1; //当前页
		loadStatus.value = 'loading'; //加载更多
		getIndexList() //根据关键字查询瀑布流
	}
	
	const remove = ((id) => {
		uWaterfall1.value.remove(id);
	})
	
	const clear = (()=> {
		uWaterfall1.value.clear();
	})

	onMounted(() => {
		console.log(`onMounted triggered`)
	})
	onReady(() => {
		getIndexList() //读取首页数据
	})
	//触底加载
	onReachBottom(() => {
		console.log('触底加载更多')
		// 如果当前页数大于等于总页数，状态修改为没有更多了，不再继续往下执行代码
		if (currentPage.value >= pages.value) {
			loadStatus.value = 'nomore';
			return;
		};
		loadStatus.value = 'loading'; //状态改为加载中
		currentPage.value = ++currentPage.value
		//修改页数后，重新获取数据
		getIndexList()
	})
</script>

<style lang="scss" scoped>
	.swip-container {
		height: 240px;
	}

	.search-container {
		background-color: #f5f5f5;
		position: sticky;
		top: 0;
		z-index: 99;
	}

	.search {
		margin: 8px;
	}

	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>